<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Construct a phar archive from an iterator</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="phar.buildfromdirectory.html">« Phar::buildFromDirectory</a></li>
      <li style="float: right;"><a href="phar.cancompress.html">Phar::canCompress »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.phar.html">Phar</a></li>
    <li>Construct a phar archive from an iterator</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="phar.buildfromiterator" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Phar::buildFromIterator</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::buildFromIterator</span> &mdash; <span class="dc-title">Construct a phar archive from an iterator</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-phar.buildfromiterator-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Phar::buildFromIterator</strong></span>(<span class="methodparam"><span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span> <code class="parameter">$iterator</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$baseDirectory</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type">array</span></div>

  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">此方法需要
将 <var class="filename">php.ini</var> 中的 <code class="literal">phar.readonly</code> 设为
<code class="literal">0</code> 以适合 <span class="classname"><a href="class.phar.html" class="classname">Phar</a></span>
对象.  否则, 将抛出<span class="classname"><a href="class.pharexception.html" class="classname">PharException</a></span>.</p></p></blockquote>
  <p class="para">
   Populate a phar archive from an iterator.  Two styles of iterators are supported,
   iterators that map the filename within the phar to the name of a file on disk,
   and iterators like DirectoryIterator that return
   SplFileInfo objects.  For iterators that return SplFileInfo objects, the second
   parameter is required.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-phar.buildfromiterator-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">iterator</code></dt>

     <dd>

      <p class="para">
       Any iterator that either associatively maps phar file to location or
       returns SplFileInfo objects
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">baseDirectory</code></dt>

     <dd>

      <p class="para">
       For iterators that return SplFileInfo objects, the portion of each
       file&#039;s full path to remove when adding to the phar archive
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-phar.buildfromiterator-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>Phar::buildFromIterator()</strong></span> returns an associative array
   mapping internal path of file to the full path of the file on the
   filesystem.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-phar.buildfromiterator-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   This method returns <span class="classname"><a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a></span> when the
   iterator returns incorrect values, such as an integer key instead of a
   string, a <span class="classname"><a href="class.badmethodcallexception.html" class="classname">BadMethodCallException</a></span> when an
   SplFileInfo-based iterator is passed without a <code class="parameter">baseDirectory</code>
   parameter, or a <span class="classname"><a href="class.pharexception.html" class="classname">PharException</a></span> if there were errors
   saving the phar archive.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-phar.buildfromiterator-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.1.0</td>
      <td>
       <span class="methodname"><strong>Phar::buildFromIterator()</strong></span> no longer returns <strong><code>false</code></strong>.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">baseDirectory</code> is now nullable.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-phar.buildfromiterator-examples">
  <h3 class="title">范例</h3>
   <div class="example" id="example-719">
    <p><strong>示例 #1 A <span class="function"><strong>Phar::buildFromIterator()</strong></span> with SplFileInfo</strong></p>
  <div class="example-contents"><p>
   For most phar archives, the archive will reflect an actual directory layout, and
   the second style is the most useful.  For instance, to create a phar archive
   containing the files in this sample directory layout:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents screen">
    <div class="examplescode"><pre class="examplescode">/path/to/project/
                 config/
                        dist.xml
                        debug.xml
                 lib/
                     file1.php
                     file2.php
                 src/
                     processthing.php
                 www/
                     index.php
                 cli/
                     index.php</pre>
</div>
   </div>
  </p></div>
  <div class="example-contents"><p>
   This code could be used to add these files to the &quot;project.phar&quot; phar archive:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;with&nbsp;alias&nbsp;"project.phar"<br /></span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveDirectoryIterator</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/project'</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'/path/to/project'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli/index.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'www/index.php'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p></div>
  <div class="example-contents"><p>
   The file project.phar can then be used immediately.  <span class="function"><strong>Phar::buildFromIterator()</strong></span> does not
   set values such as compression, metadata, and this can be done after creating the
   phar archive.
  </p></div>
  <div class="example-contents"><p>
   As an interesting note, <span class="function"><strong>Phar::buildFromIterator()</strong></span> can also be used to
   copy the contents of an existing phar archive, as the Phar object descends
   from <span class="classname"><a href="class.directoryiterator.html" class="classname">DirectoryIterator</a></span>:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;with&nbsp;alias&nbsp;"project.phar"<br /></span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/anotherphar.phar'</span><span style="color: #007700">)),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'phar:///path/to/anotherphar.phar/path/to/project'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli/index.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'www/index.php'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p></div>
  </div>
  <div class="example" id="example-720">
    <p><strong>示例 #2 A <span class="function"><strong>Phar::buildFromIterator()</strong></span> with other iterators</strong></p>
  <div class="example-contents"><p>
   The second form of the iterator can be used with any iterator that returns
   a key =&gt; value mapping, such as an <span class="classname"><a href="class.arrayiterator.html" class="classname">ArrayIterator</a></span>:
  </p></div>
  <div class="example-contents"><p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;with&nbsp;alias&nbsp;"project.phar"<br /></span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'project.phar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">buildFromIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'internal/file.php'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'/somefile.php'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'another/file.jpg'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/bigfile.jpg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)));<br /></span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setStub</span><span style="color: #007700">(</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDefaultStub</span><span style="color: #007700">(</span><span style="color: #DD0000">'cli/index.php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'www/index.php'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </p></div>
  </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-phar.buildfromiterator-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="phar.buildfromdirectory.html" class="function" rel="rdfs-seeAlso">Phar::buildFromDirectory()</a> - Construct a phar archive from the files within a directory</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>